package com.zusmart.base.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;

public abstract class StringUtils {

	private static final String NULL_OBJECT = "null_object";
	private static final String NULL_ERRORS = "unknown_exception";
	private static final String PACKAGE_SEPARATOR_CHAR = ".";

	/**
	 * 判断字符串是否为空
	 * 
	 * @param value
	 * @return true为空,false不为空
	 */
	public static boolean isBlank(CharSequence value) {
		int length;
		if (null == value || (length = value.length()) == 0) {
			return true;
		}
		for (int i = 0; i < length; i++) {
			if (!Character.isWhitespace(value.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断字符串是否不为空
	 * 
	 * @param value
	 * @return true你不为空,false为空
	 */
	public static boolean isNotBlank(CharSequence value) {
		return !isBlank(value);
	}

	/**
	 * 字符串拼接
	 * 
	 * @return 拼接后的字符串
	 */
	public static String concat(CharSequence... sequence) {
		StringBuilder sb = new StringBuilder();
		for (CharSequence cs : sequence) {
			sb.append(cs);
		}
		return sb.toString();
	}

	/**
	 * 转换为骆驼命名法
	 * 
	 * @param value
	 *            字符串
	 * @return 骆驼命名法处理后的字符串
	 */
	public static String toCamelName(String value) {
		if (isBlank(value)) {
			return value;
		}
		return value.substring(0, 1).toLowerCase() + value.substring(1);
	}

	/**
	 * 获取对象的简单类名
	 * 
	 * @param value
	 *            对象信息
	 * @return 为空返回null_object,不为空返回对应的简单类名
	 */
	public static String getSimpleClassName(Object value) {
		if (null == value) {
			return NULL_OBJECT;
		} else {
			return getSimpleClassName(value.getClass());
		}
	}

	/**
	 * 获取类的简单类名
	 * 
	 * @param clazz
	 *            类
	 * @return 简单类名
	 * @throws IllegalArgumentException
	 *             class为空时抛出 {@link IllegalArgumentException}
	 */
	public static String getSimpleClassName(Class<?> clazz) throws IllegalArgumentException {
		Assert.isNull(clazz, "clazz must not be null");
		String className = clazz.getName();
		final int idx = className.lastIndexOf(PACKAGE_SEPARATOR_CHAR);
		if (idx > -1) {
			return className.substring(idx + PACKAGE_SEPARATOR_CHAR.length());
		}
		return className;
	}

	/**
	 * 返回指定异常信息
	 * 
	 * @param cause
	 *            异常对象
	 * @return 异常信息,cause为空时,返回unknown exception
	 */
	public static String getExceptionMessage(Throwable cause) {
		if (null == cause) {
			return NULL_ERRORS;
		}
		if (isBlank(cause.getMessage())) {
			return cause.toString();
		} else {
			return cause.getMessage();
		}
	}

	/**
	 * 集合类型转string数组
	 * 
	 * @param collection
	 *            集合内容
	 * @return 字符串数组
	 */
	public static String[] toArray(Collection<String> collection) {
		return collection.toArray(new String[0]);
	}

	/**
	 * 字符串根据分隔符转string数组
	 * 
	 * @param str
	 *            字符串
	 * @param delimiters
	 *            分隔符
	 * @param trimTokens
	 *            是否去掉空格
	 * @param ignoreEmptyTokens
	 *            是否忽略两个分隔符中间只有空格的数据
	 * @return 字符串数组
	 */
	public static String[] toArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens) {
		if (str == null) {
			return new String[0];
		}
		StringTokenizer st = new StringTokenizer(str, delimiters);
		List<String> tokens = new ArrayList<String>();
		while (st.hasMoreTokens()) {
			String token = st.nextToken();
			if (trimTokens) {
				token = token.trim();
			}
			if (!ignoreEmptyTokens || token.length() > 0) {
				tokens.add(token);
			}
		}
		return toArray(tokens);
	}

	/**
	 * 替换字符串内容
	 * 
	 * @param value
	 *            目标字符串
	 * @param oldPattern
	 *            需要替换的字符串
	 * @param newPattern
	 *            替换的为字符串
	 * @return 替换后的结果字符串
	 */
	public static String replace(String value, String oldPattern, String newPattern) {
		if (isBlank(value) || isBlank(oldPattern) || null == newPattern) {
			return value;
		}
		int index = value.indexOf(oldPattern);
		if (index == -1) {
			return value;
		}
		int capacity = value.length();
		if (newPattern.length() > oldPattern.length()) {
			capacity += 16;
		}
		StringBuilder sb = new StringBuilder(capacity);
		int pos = 0;
		int len = oldPattern.length();
		while (index >= 0) {
			sb.append(value.substring(pos, index));
			sb.append(newPattern);
			pos = index + len;
			index = value.indexOf(oldPattern, pos);
		}
		sb.append(value.substring(pos));
		return sb.toString();
	}

}